
$(function(){

    var pageResult = 0,pageCount = 2,w = $(window).width();

    var latestObj = $('#channel-latest'),allObj = $('#channel-all');

    //默认进入最近模块
    renderImg();

    //图片渲染
    function renderImg(){
        //图片间隔宽度
        //imgListWidth(2,3);
        app.ajax('get',app.url('getImgList.php'),{page:pageCount,pageStart:pageResult},function(data){
            var obj = data,str = '',page = '';
            var model = $('#model-1').clone();
            var imgListModel = $("#model-1-a").clone();
            for(var i=0;i<obj.length;i++){
                var imgStr = '';
                if(obj[i].statusCode == 404){continue;}
                for(var j=0;j<obj[i].data.length;j++){
                    //时间
                    model.find("[data-name='time']").text(obj[i].data[j].time);
                    imgListModel.find('.img-list').css('background-image',"url("+app.imgUrl('photo/'+obj[i].data[j].imgPath)+")").attr('data-id',obj[i].data[j].id);
                    imgStr += imgListModel.html();
                }
                //向内存中的dom模板插入图片数列表
                model.find("[data-name='imgPath']").html(imgStr);
                for(var k=0;k<model.find('.img-list').length;k++){
                    if(k%3!=0){ model.find('.img-list').eq(k).css('margin-left','.5%') }
                }
                //保存对象列表字符串到变量
                str +=  model.html();
                page = obj[i].page;
            }

            if(str == ''){
                $.comMessage({
                    type:'prompt',
                    text:'最近只有这些哦!',
                    time:3000,
                    animateTop:'80px'
                });
                return;
            }else{
                latestObj.append(str);
            }
            return pageResult =  page;
        })
    }

    //==================================================================

    //图片大小和边距
    function imgListWidth(param,num){
        var liw = (w-param*(num-1))/num - 0.1;
        $('#model-1-a').find('.img-list').css({
            width:liw + 'px',
            height:liw + 'px',
        });
    }

    //初始化页面
    function initPage(n){
        pageCount = 2;
        pageResult = 0;
        pagesCount = 9;
        pagesResult = 0;
        n == 0 ?  latestObj.html('') : allObj.children("[data-name='imgPath']").html('');
    }

    //二级页面===========================================================

    //调用图片插件

    //全部模块********************************************************
    var pagesResult = 0,pagesCount = 9;
    function renderAllImg(){
        //imgListWidth(2,3)
        app.ajax('get',app.url('getImgListAll.php'),{page:pagesCount,pageStart:pagesResult},function(data){
            var obj = data,imgStr='';
            var imgListModel = $("#model-1-a").clone();
            for(var i=0;i<obj.length;i++){
                if(obj[i].statusCode == 404){continue;}
                imgListModel.find('.img-list').css('margin-left','');
                imgListModel.find('.img-list').css('background-image',"url("+app.imgUrl('photo/'+obj[i].imgPath)+")").attr('data-id',obj[i].id);
                if(i%3!=0){ imgListModel.find('.img-list').css('margin-left','.5%') }
                imgStr += imgListModel.html();
            }

            if(imgStr == ''){
                $.comMessage({
                    type:'prompt',
                    text:'最近只有这些哦!',
                    time:3000,
                    animateTop:'80px'
                })
            }else{
                //将变量字符串插入真实dom
                allObj.children("[data-name='imgPath']").append(imgStr);
            }
            pagesResult += 9;
        });
    }

    //最近模块和全部模块切换
    $('.item-list').on('tap',function(){
        console.log(111);
        var index = $(this).index();
            allObj.hide();
            latestObj.hide();
        $(this).addClass('active');
            if(index == 0 ){
                initPage(0);
                renderImg();
                latestObj.show();
                $('.item-list').eq(1).removeClass('active')
            }else{
                initPage(1);
                renderAllImg();
                allObj.show();
                $('.item-list').eq(0).removeClass('active')
            }
    });

    //加载更多
    $('.load-button').on('tap',function(){
        if(latestObj.css('display') == 'none'){
            renderAllImg()
        }else{ renderImg(); }
    })



    //==========================================================
    //调用组件
    var imgObj = null;
    imgObj = new Album({
        currentObj:$('.album'),
        speed:400,
        //回退按钮回调函数
        callback:null,
        scale:false
    });

    $(".container").on('tap','.img-list',function(){
        var _this = this,imgId = '',imgArr = [],str = '';
        //渲染组件
        var pageImgObj = $(this).parent("[data-name='imgPath']").find('.img-list');
            pageImgObj.each(function(index,obj){
                if(obj == _this){imgId = index}
                var imgPath = obj.style.backgroundImage;
                imgPath = imgPath.match(/(http.+\.(jpg|png))/)[1];
                imgArr.push(imgPath);
            });
            var imgModel = $('#model-2').clone();
            $(imgArr).each(function(index,obj){
                imgModel.find('img').attr('src',obj);
                str += imgModel.html()
            });
            $(".album-item").html(str);

        imgObj.init(imgId,imgArr.length);
    });


})